<?php
namespace app\admin\controller;
use app\admin\model\Address;

class ResidentialQuarters extends BaseAdmin{
    public function lists(){
        $s = $this->db->table('address')->distinct(true)->field('p1,a1')->lists();
        $s_html = '<option value="">请选择省</option>';
        foreach ($s as $k =>$v) {
            $s_html .= '<option value="'.$v['p1'].'">'.$v['a1'].'</option>';
        }
        $this->assign('s_html',$s_html);
        return $this->fetch();
    }

    //查询房源
    public function check(){
        $limit=input("get.limit")?input("get.limit"):10;
        $page=input("get.page")?input("get.page"):1;
        $limit=intval($limit);
        $page=intval($page);
        $start=$limit*($page-1);
        $where =array();
        !empty($_GET['rq_name'])&&$where[]=['rq_name','like','%'.$_GET['rq_name'].'%'];
//        !empty($_GET['bc_area_id'])&&$where[]=['bc_area_id','=',$_GET['bc_area_id']];
       // dump($where);
        $house = $this->db->table('residential_quarters')->where($where)->limit($start)->order('rq_id', 'desc')->pages($limit);
//        dump($where);die;
        $count = $house["total"];
        $data =array(
            'code'=>'0',
            'msg'=>'0',
            'count'=>$count,
            'data'=>array(),
        );
        $address = $this->get_address(false);
        $business_circle = $this->get_business_circle(false);
        foreach ($house['lists'] as $k => $v){
            $v['operation'] = '
                <button class="layui-btn layui-btn-xs" id="'.$v['rq_id'].'"  lay-submit lay-filter="editor" style="display: none">编辑</button>
                <button class="layui-btn layui-btn-danger layui-btn-xs" id="'.$v['rq_id'].'"  lay-submit lay-filter="del">删除</button>
';
            if(!empty($v['rq_create_time'])){
                $v['rq_create_time'] = date("Y-m-d H:i:s",(int)$v['rq_create_time']);
            }
            if(!empty($v['rq_bc_id'])){
                $v['rq_bc_id'] = $business_circle[$v['rq_bc_id']]['bc_name'];
            }

            $data['data'][] =$v;
        }
        exit(json_encode($data));

    }




    //删除小区
    public function del(){
        empty($_POST['rq_id'])&&$this->Tool->Tips(1,'参数错误',$_POST);
        $del = $this->db->table('residential_quarters')->where(array('rq_id' => input('post.rq_id')))->delete();
        //dump($del);
        if ($del) {
            $this->Tool->Tips(0,'删除成功',$_POST);
        } else {
            $this->Tool->Tips(1,'删除失败',$del);
        }
    }

    //添加小区
    public function add(){
        $s = $this->db->table('business_circle')->lists();
        $s_html = '<option value="">请选商圈</option>';
        foreach ($s as $k =>$v) {
            $s_html .= '<option value="'.$v['bc_id'].'">'.$v['bc_name'].'</option>';
        }
        $this->assign('s_html',$s_html);
        return $this->fetch();
    }

    //保存
    public function save(){
        if (empty($_POST['rq_bc_id'])||empty($_POST['rq_name'])) {
            $this->Tool->Tips(1,'参数错误',$_POST);
        }
        $data = [
            'rq_bc_id'=>$_POST['rq_bc_id'],
            'rq_name'=>$_POST['rq_name'],
            'rq_create_time'=>time(),
        ];
        $id = $this->db->table('residential_quarters')->insert($data);
        if($id){
            $this->Tool->Tips(0,'保存成功');
        }
        $this->Tool->Tips(1,'保存失败');
    }

//    public function status(){
//        if(session('admin')['gid']==1||session('admin')['gid']==2) {
//            //exit(json_encode($_POST));
//            $_POST['status'] = $_POST['status']=='true'?0:1;
//            $save = $this->db->table('admin_sales')->where(array('id'=>$_POST['id']))->update($_POST);
//            if($save){
//                exit(json_encode(array('code'=>0,'msg'=>'修改成功')));
//            }else{
//                exit(json_encode(array('code'=>1,'msg'=>'修改失败')));
//            }
//        }else{
//            exit(json_encode(array('code' => 1, 'msg' => '您暂无权限此操作')));
//        }
//
//    }


    public function shi(){
        if($_POST['p1']){
            $shi = $this->db->table('address')->where(['p1'=>$_POST['p1']])->distinct(true)->field('p2,a2')->lists();
        }else{
            $this->Tool->Tips(1,'参数错误');
        }
        if($shi){
            $html = '<option value="">请选择市</option>';
            foreach ($shi as $k=>$v) {
                $html .= '<option value="'.$v['p2'].'">'.$v['a2'].'</option>';
            }
            $this->Tool->Tips(0,'查询成功',$html);
        }
        $this->Tool->Tips(1,'未查询到数据');
    }

    public function qu(){
        if($_POST['p2']){
            $shi = $this->db->table('address')->where(['p2'=>$_POST['p2']])->distinct(true)->field('a4,a3')->lists();
        }else{
            $this->Tool->Tips(1,'参数错误');
        }
        if($shi){
            $html = '<option value="">请选择市</option>';
            foreach ($shi as $k=>$v) {
                $html .= '<option value="'.$v['a4'].'">'.$v['a3'].'</option>';
            }
            $this->Tool->Tips(0,'查询成功',$html);
        }
        $this->Tool->Tips(1,'未查询到数据');
    }


    public function upload(){
        $this->upload_img();
    }
}